# 一小段js逆向


# 1.确定数据的来源
# 2.
import requests
import time
import execjs

word = input('請輸入你要翻譯的内容')
f = open('youdao.js', encoding='utf-8')  # 讀取我們的代碼
js_code = f.read()
compile_code = execjs.compile(js_code)
js_data = compile_code.call('fanyi', word)
print(js_data)
url = 'https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
    'Cookie': 'OUTFOX_SEARCH_USER_ID_NCOO=437880852.2670789; OUTFOX_SEARCH_USER_ID="419573624@10.110.96.154"; fanyi-ad-id=307888; fanyi-ad-closed=1; ___rl__test__cookies=1657290797717',
    'Host': 'fanyi.youdao.com',
    'Origin': 'https://fanyi.youdao.com',
    'Referer': 'https://fanyi.youdao.com/'
}

data = {
    'i': word,
    'from': 'AUTO',
    'to': ' AUTO',
    'smartresult': 'dict',
    'client': 'fanyideskweb',
    'salt': js_data['salt'],
    'sign': js_data['sign'],
    'lts': js_data['ts'],
    'bv': js_data['bv'],
    'doctype': 'json',
    'version': '2.1',
    'keyfrom': 'fanyi.web',
    'action': 'FY_BY_REALTlME',
}
res = requests.post(url=url, headers=headers, data=data)
eng = res.json()['translateResult'][0][0]['tgt']
